package com.leetcode.code;

import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 *
 * @author zkingcobra
 * Date: 2020/1/15 3:44 下午
 */
public class LeetCode0013Java {
    public static void main(String[] args) {
        Solution solution = new Solution();

        System.out.println(solution.romanToInt2("MCMXCIV"));
        System.out.println(solution.romanToInt2("III"));
    }

    static class Solution {
        /**
         * 方法一
         * 执行用时 :11 ms, 在所有 Java 提交中击败了31.93%的用户
         * 内存消耗 :37.8 MB, 在所有 Java 提交中击败了89.44%的用户
         */
        public int romanToInt(String s) {

            if (s == null || s.length() == 0) {
                return 0;
            }

            String tempAll = s;

            Map<String, Integer> map = new HashMap();
            map.put("I", 1);
            map.put("V", 5);
            map.put("X", 10);
            map.put("L", 50);
            map.put("C", 100);
            map.put("D", 500);
            map.put("M", 1000);
            map.put("CM", 900);
            map.put("CD", 400);
            map.put("IX", 9);
            map.put("XL", 40);
            map.put("XC", 90);
            map.put("IV", 4);

            int answer = 0;
            int size = s.length();
            for (int index = 0; index < size; ) {
                int tempStep = 1;
                if (index + 1 < size && map.containsKey(s.substring(index, index + 2))) {
                    tempStep = 2;
                }

                String tempValue = s.substring(index, (index + tempStep));
                answer += map.get(tempValue);
                index += tempStep;
            }

            return answer;
        }

        /**
         * 方法二
         * 执行用时 :4 ms, 在所有 Java 提交中击败了99.96%的用户
         *
         * 内存消耗 :35.9 MB, 在所有 Java 提交中击败了99.59%的用户
         */
        public int romanToInt2(String s) {
            int result = 0;
            int len = s.length();
            for (int i = 0; i < len; i++) {
                char at = s.charAt(i);
                switch (at) {
                    case 'M':
                        result += 1000;
                        break;
                    case 'D':
                        result += 500;
                        break;
                    case 'C':
                        if (i < len - 1 && (s.charAt(i + 1) == 'D' || s.charAt(i + 1) == 'M')) {
                            result -= 100;
                        } else {
                            result += 100;
                        }
                        break;
                    case 'L':
                        result += 50;
                        break;
                    case 'X':
                        if (i < len - 1 && (s.charAt(i + 1) == 'L' || s.charAt(i + 1) == 'C')) {
                            result -= 10;
                        } else {
                            result += 10;
                        }
                        break;
                    case 'V':
                        result += 5;
                        break;
                    case 'I':
                        if (i < len - 1 && (s.charAt(i + 1) == 'V' || s.charAt(i + 1) == 'X')) {
                            result -= 1;
                        } else {
                            result += 1;
                        }
                        break;
                    default:
                        break;
                }
            }

            return result;
        }

        /**
         * 方法三
         * 执行用时 :4 ms, 在所有 Java 提交中击败了99.96%的用户
         *
         * 内存消耗 :36.3 MB, 在所有 Java 提交中击败了97.24%的用户
         */
        public int romanToInt3(String s) {
            if (null == s || s.length() == 0) {
                return 0;
            }

            int res = 0;
            int pre = 0;
            for (int i = s.length() - 1; i >= 0; i--) {
                int cur = getNum(s.charAt(i));
                if (cur >= pre) {
                    res += cur;
                } else {
                    res -= cur;
                }
                pre = cur;
            }
            return res;
        }

        private int getNum(char c) {
            int res = 0;
            switch (c) {
                case 'I':
                    res = 1;
                    break;
                case 'V':
                    res = 5;
                    break;
                case 'X':
                    res = 10;
                    break;
                case 'L':
                    res = 50;
                    break;
                case 'C':
                    res = 100;
                    break;
                case 'D':
                    res = 500;
                    break;
                case 'M':
                    res = 1000;
                    break;
                default:
                    res = -1;
            }
            return res;
        }
    }
}
